package com.scs.application.modules.wm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UtilNum;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.wm.dto.PickItemGroupByApplyPackageUnitDTO;
import com.scs.application.modules.wm.entity.DistItem;
import com.scs.application.modules.wm.entity.DistItemPda;
import com.scs.application.modules.wm.mapper.PickItemMapper;
import com.scs.application.modules.wm.service.DistItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 科室请领单明细 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/wm/dist_item")
public class DistItemController extends CrudController<DistItemService, DistItem>{
    @Autowired
    PickItemMapper pickItemMapper;



    @PostMapping("/cancelPick")
    public RestResult cancelPick(@RequestParam(name = "distId") String distId) {
        service.cancelPick(distId);
        return RestResult.ok();
    }

    @PostMapping("/endPick")
    public RestResult endPick(@RequestParam(name = "distId") String distId) {
        service.endPick(distId);
        return RestResult.ok();
    }

    @GetMapping("/findDistItemPda")
    public RestResult findDistItemPda() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        List<DistItemPda> distItemPda = service.findDistItemPda(queryContext);
        if (distItemPda == null || distItemPda.size() < 1)  return RestResult.ok("");

        List<String> itemIdList = distItemPda.stream().map(ob -> ob.getId()).collect(Collectors.toList());

        QueryWrapper queryWrapperPickItem = Wrappers.query().in("dist_item_id", itemIdList);
        List<PickItemGroupByApplyPackageUnitDTO> pickItemGroupList=pickItemMapper.getListGroupByDistItemIdPackageUnit(queryWrapperPickItem);


        List<String> msgArr = new ArrayList<String>();
        for(DistItemPda dip:distItemPda){
            dip.setPickPackageQty(dip.getPickedSkuQty()/dip.getPackageRate());

            //已拣货包装列表
            if (pickItemGroupList != null && pickItemGroupList.size() > 0){
                List<PickItemGroupByApplyPackageUnitDTO> findList = pickItemGroupList.stream()
                        .filter(pickItemGroup -> dip.getId().equals(pickItemGroup.getDistItemId()) )
                        .collect(Collectors.toList());
                if (findList != null && findList.size() > 0){
                    msgArr.clear();
                    findList.stream().forEach(stockGroupWarehouseMatrDTO -> msgArr.add(UtilNum.trimZero(stockGroupWarehouseMatrDTO.getPackageQty())+stockGroupWarehouseMatrDTO.getPackageUnit()));
                    dip.setPickMsg(StringUtils.join(msgArr,","));
                }else {
                    dip.setPickMsg("无");
                }
            }
        }
        return RestResult.ok("",distItemPda);
    }


    /**
    * @Description： 批量添加
    */
    @PostMapping("/addDtlBatch")
    public RestResult addDtlBatch(@Valid @RequestBody List<DistItem> entityList) {
        service.addDtlBatch(entityList);
        return RestResult.ok("批量添加成功",  entityList);
    }
}
